package com.dharma.mydiet.view;

import android.app.ActionBar;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.dharma.mydiet.MainApplication;
import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.DAOUser;
import com.dharma.mydiet.storage.TransactionalUser;
import com.dharma.mydiet.utility.Utils;
import com.dharma.mydiet.utility.Utils.TextFieldType;
import com.google.analytics.tracking.android.EasyTracker;

public class MyProfileActivity extends Activity {

    private Button save;
    private TextView textUserName;
    private TextView textPassword;
    private TextView textName;
    private TextView textEmailAddress;
    private TextView textDateBorn;
    private TextView textEmergencyPhone;
    private TextView textHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.activity_new_user);

	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());
	ActionBar ab = getActionBar();
	ab.setSubtitle("Datos de mi perfil");

	save = (Button) findViewById(R.id.saveButton);
	save.setText("Actualizar");

	textUserName = (TextView) findViewById(R.id.textUserName);
	textPassword = (TextView) findViewById(R.id.textPassword);
	textName = (TextView) findViewById(R.id.textName);
	textEmailAddress = (TextView) findViewById(R.id.textEmailAddress);
	((TextView) findViewById(R.id.textDateBorn)).setVisibility(View.GONE);
	((TextView) findViewById(R.id.born)).setVisibility(View.GONE);
	textEmergencyPhone = (TextView) findViewById(R.id.textEmergencyPhone);
	textHeight = (TextView) findViewById(R.id.textHeight);
	((TextView) findViewById(R.id.textWeight)).setVisibility(View.GONE);
	((TextView) findViewById(R.id.weight)).setVisibility(View.GONE);

	setValues();

	if (save != null) {
	    save.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
		    if (isValidEntry()) {
			TransactionalUser user = new TransactionalUser(0, textName.getText().toString(), "",
				((MainApplication) getApplication()).getCurrentUser().getAge(), Float
					.parseFloat(textHeight.getText().toString()), textEmailAddress.getText()
					.toString(), textEmergencyPhone.getText().toString(), textPassword.getText()
					.toString(), textUserName.getText().toString());
			DAOUser.getInstance().updateUser(user);
			finish();
		    }
		}
	    });
	}
    }

    private void setValues() {
	TransactionalUser user = ((MainApplication) getApplication()).getCurrentUser();
	textUserName.setText(user.getAlias());
	textPassword.setText(user.getPassword());
	textName.setText(user.getName());
	textEmailAddress.setText(user.getEmail());
	textEmergencyPhone.setText(user.getTelefonoUrgencias());
	textHeight.setText("" + (int) user.getHeight());
    }

    public boolean isValidEntry() {
	boolean isValid = true;
	if (!Utils.isValidTextField(TextFieldType.NAME, textUserName.getText().toString())) {
	    textUserName.setError("Entrada Invalida.");
	    isValid = false;
	}
	if (!Utils.isValidTextField(TextFieldType.NAME, textPassword.getText().toString())) {
	    textPassword.setError("Entrada Invalida.");
	    isValid = false;
	}
	if (!Utils.isValidTextField(TextFieldType.NAME, textName.getText().toString())) {
	    textName.setError("Entrada Invalida.");
	    isValid = false;
	}
	if (!Utils.isValidTextField(TextFieldType.EMAIL, textEmailAddress.getText().toString())) {
	    textEmailAddress.setError("Entrada Invalida.");
	    isValid = false;
	}
	if (textEmergencyPhone.getText() != null && !textEmergencyPhone.getText().equals("")
		&& !Utils.isValidTextField(TextFieldType.PHONE, textEmergencyPhone.getText().toString())) {
	    textEmergencyPhone.setError("Entrada Invalida.");
	    isValid = false;
	}
	if (!Utils.isValidTextField(TextFieldType.NUMBER_FLOAT, textHeight.getText().toString())) {
	    textHeight.setError("Entrada Invalida.");
	    isValid = false;
	}
	return isValid;
    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
